//
//  ImageFolderView.m
//  GoFactory
//
//  Created by wtb on 2021/9/29.
//  Copyright © 2021 KQZK. All rights reserved.
//

#import "ImageFolderView.h"
#import "WTAssetCell.h"

@interface ImageFolderView()
<
UITableViewDelegate,
UITableViewDataSource
>
@property(nonatomic,strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *albumArr;
@end

@implementation ImageFolderView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame ]) {
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
        self.isFirstAppear = YES;
        [self configTableView];
    }
    return self;
}

//-(UITableView *)tableView{
//    if (!_tableView) {
//        _tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
//        _tableView.backgroundColor = [UIColor clearColor];
//        _tableView.delegate = self;
//        _tableView.dataSource = self;
//        _tableView.rowHeight = 120;
//        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    }
//    return _tableView;
//}

- (void)configTableView {
    if (![[TZImageManager manager] authorizationStatusAuthorized]) {
        return;
    }

    if (self.isFirstAppear) {
//        [self.imagePickerVc showProgressHUD];
    }

    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [[TZImageManager manager] getAllAlbumsWithFetchAssets:!self.isFirstAppear completion:^(NSArray<TZAlbumModel *> *models) {
            dispatch_async(dispatch_get_main_queue(), ^{
                self->_albumArr = [NSMutableArray arrayWithArray:models];
                for (TZAlbumModel *albumModel in self->_albumArr) {
                    albumModel.selectedModels = self.imagePickerVc.selectedModels;
                }
                [self.imagePickerVc hideProgressHUD];
                
                if (self.isFirstAppear) {
                    self.isFirstAppear = NO;
                    [self configTableView];
                }
                
                if (!_tableView) {
                    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 18, self.bounds.size.width, self.bounds.size.height-18) style:UITableViewStylePlain];
                    _tableView.rowHeight = 104;
                    _tableView.backgroundColor = [UIColor clearColor];
                    _tableView.dataSource = self;
                    _tableView.delegate = self;
                    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
                    [_tableView registerClass:[WTAlbumCell class] forCellReuseIdentifier:@"TZAlbumCell"];
                    [self addSubview:self->_tableView];
                    if (self.imagePickerVc.albumPickerPageUIConfigBlock) {
                        self.imagePickerVc.albumPickerPageUIConfigBlock(self->_tableView);
                    }
                } else {
                    [self->_tableView reloadData];
                }
            });
        }];
    });
}

#pragma mark - UITableViewDataSource && Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _albumArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WTAlbumCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TZAlbumCell"];
//    if (@available(iOS 13.0, *)) {
//        cell.backgroundColor = UIColor.tertiarySystemBackgroundColor;
//    }
//    cell.albumCellDidLayoutSubviewsBlock = self.imagePickerVc.albumCellDidLayoutSubviewsBlock;
//    cell.albumCellDidSetModelBlock = self.imagePickerVc.albumCellDidSetModelBlock;
    cell.selectedCountButton.backgroundColor = self.imagePickerVc.iconThemeColor;
    cell.model = _albumArr[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.selectPhotoItem) {
        [self configTableView];
        self.selectPhotoItem(self.columnNumber, _albumArr[indexPath.row]);
    }
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
@end
